/**
 * 
 */
package profile.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;


/**
 * @author ductrung.tran
 *
 */
@Aspect
public abstract class JunitTestContextProviderAspect {

	@Pointcut
	public abstract void testMethodPointcut();
	
	@Around("testMethodPointcut()")
	public Object testMethodAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
		try {
			setUpTestContext(joinPoint);
			return joinPoint.proceed();
		} finally {
			tearDownTestContext();
		}
	}

	private void setUpTestContext(ProceedingJoinPoint joinPoint) {
		CallContext context = new CallContext(joinPoint.getThis().getClass().getSimpleName()+ "." + joinPoint.getSignature().getName());
		CallContextHolder.setContext(context);
	}

	private void tearDownTestContext() {
		CallContextHolder.removeContext();
	}
}
